﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;    //导入解析Json的类  
public partial class Get : System.Web.UI.Page
{
    static CookieContainer myCookieContainer = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        GetDataSetFromWebServiceByXmlDocument();
    }
   
    public class ParcelInfo
    {
        public String boxId;
        public String courierCorp;
        public String courierId;
        public String getTime;
        public String packageId;
        public String packageStatus;
        public String parcelId;
        public String storeTime;
        public String terminalId;
        public String userPhone;
    }

    /// <summary>
    /// WebService返回的是XmlDocument
    /// </summary>
    /// 
    static void GetDataSetFromWebServiceByXmlDocument()
    {
        string webserviceUrl = "http://service.24box.cn:21198/YouBoxAdmin/login?username=admin&password=admin&returnType=json";
        string charSet = "utf-8";

        try
        {
            string pageContent = GetWebServiceContent(webserviceUrl, charSet);
            Console.WriteLine(pageContent);

            webserviceUrl = "http://service.24box.cn:21198/YouBoxAdmin/packageDetailByParcelID?parcelID=1900420032323&returnType=json";

            pageContent = GetWebServiceContent(webserviceUrl, charSet);
            pageContent = pageContent.Replace("\"", "'");
            string te = @"[{'boxId':2,'courierCorp':'快递员','courierId':16,
'getTime':'2013-06-03T16:59:28','packageId':2326,
'packageStatus':1,'parcelId':'1900420032323',
'storeTime':'2013-06-03T16:33:28','terminalId':3,
'userPhone':13696837370},{'boxId':2,'courierCorp':'快递员','courierId':16,
'getTime':'2013-06-03T16:59:28','packageId':2326,
'packageStatus':1,'parcelId':'1900420032323',
'storeTime':'2013-06-03T16:33:28','terminalId':3,
'userPhone':13696837370},{'boxId':2,'courierCorp':'快递员','courierId':16,
'getTime':'2013-06-03T16:59:28','packageId':2326,
'packageStatus':1,'parcelId':'1900420032323',
'storeTime':'2013-06-03T16:33:28','terminalId':3,
'userPhone':13696837370}]";
            string te1 = @"[{'boxId':12,'courierCorp':'顺丰快递','courierId':2,'getTime':'2013-03-18T10:03:35','packageId':72,'packageStatus':1,'parcelId':'abcd12bacd','storeTime':'2013-03-18T10:01:59','terminalId':1,'userPhone':18960877052},{'boxId':12,'courierCorp':'顺丰快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':71,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-03-18T09:48:48','terminalId':1,'userPhone':18960877052},{'boxId':29,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':914,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T11:56:56','terminalId':5,'userPhone':18960877052},{'boxId':18,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':915,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T12:04:50','terminalId':5,'userPhone':18960877052},{'boxId':23,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':916,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T12:07:23','terminalId':5,'userPhone':18960877052},{'boxId':24,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':917,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T12:07:35','terminalId':5,'userPhone':18960877052},{'boxId':25,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':918,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T12:07:41','terminalId':5,'userPhone':18960877052},{'boxId':11,'courierCorp':'中通快递','courierId':5,'getTime':null,'packageId':919,'packageStatus':1,'parcelId':'1900377882051','storeTime':'2013-05-08T12:53:11','terminalId':5,'userPhone':18960877052},{'boxId':11,'courierCorp':'中通快递','courierId':5,'getTime':null,'packageId':920,'packageStatus':1,'parcelId':'1900377882051','storeTime':'2013-05-08T12:53:11','terminalId':5,'userPhone':18960877052},{'boxId':11,'courierCorp':'中通快递','courierId':5,'getTime':null,'packageId':921,'packageStatus':1,'parcelId':'1900377882051','storeTime':'2013-05-08T12:53:12','terminalId':5,'userPhone':18960877052},{'boxId':33,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':931,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T13:31:40','terminalId':5,'userPhone':18960877052},{'boxId':16,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':932,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T13:33:33','terminalId':5,'userPhone':18960877052},{'boxId':17,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':933,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T13:37:38','terminalId':5,'userPhone':18960877052},{'boxId':22,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':934,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T13:40:02','terminalId':5,'userPhone':18960877052},{'boxId':27,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':935,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T14:05:57','terminalId':5,'userPhone':18960877052},{'boxId':28,'courierCorp':'????','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':936,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T14:06:22','terminalId':5,'userPhone':18960877052},{'boxId':22,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':937,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T14:06:27','terminalId':5,'userPhone':18960877052},{'boxId':29,'courierCorp':'????','courierId':1,'getTime':null,'packageId':938,'packageStatus':1,'parcelId':'abcdd12341bacd','storeTime':'2013-05-08T14:22:43','terminalId':5,'userPhone':18960877052},{'boxId':19,'courierCorp':'汇通快递','courierId':1,'getTime':null,'packageId':939,'packageStatus':1,'parcelId':'abcdd12341bacd','storeTime':'2013-05-08T14:23:21','terminalId':5,'userPhone':18960877052},{'boxId':20,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':940,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T14:23:37','terminalId':5,'userPhone':18960877052},{'boxId':30,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':941,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T14:24:34','terminalId':5,'userPhone':18960877052},{'boxId':31,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':942,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-08T14:25:40','terminalId':5,'userPhone':18960877052},{'boxId':37,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':973,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-09T12:30:11','terminalId':5,'userPhone':18960877052},{'boxId':29,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':974,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-09T12:35:42','terminalId':5,'userPhone':18960877052},{'boxId':32,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':978,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-09T15:03:46','terminalId':5,'userPhone':18960877052},{'boxId':31,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-09T15:06:25','packageId':979,'packageStatus':1,'parcelId':'abcd1s2341bacd','storeTime':'2013-05-09T15:04:14','terminalId':5,'userPhone':18960877052},{'boxId':31,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-09T15:09:27','packageId':980,'packageStatus':1,'parcelId':'abcd1sd2341bacd','storeTime':'2013-05-09T15:07:19','terminalId':5,'userPhone':18960877052},{'boxId':36,'courierCorp':'韵达快递','courierId':6,'getTime':'2013-05-11T12:07:25','packageId':996,'packageStatus':1,'parcelId':'117107146811','storeTime':'2013-05-10T10:28:56','terminalId':5,'userPhone':18960877052},{'boxId':33,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':1032,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-11T09:42:04','terminalId':5,'userPhone':18960877052},{'boxId':33,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':1035,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-11T12:19:00','terminalId':5,'userPhone':18960877052},{'boxId':24,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':1067,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-15T11:28:13','terminalId':5,'userPhone':18960877052},{'boxId':12,'courierCorp':'汇通快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':1138,'packageStatus':1,'parcelId':'abcd12341bacd','storeTime':'2013-05-17T15:52:43','terminalId':8,'userPhone':18960877052},{'boxId':1,'courierCorp':'圆通快递','courierId':3,'getTime':'2013-05-19T13:42:35','packageId':1176,'packageStatus':1,'parcelId':'112459512327','storeTime':'2013-05-19T10:16:58','terminalId':8,'userPhone':18960877052},{'boxId':1,'courierCorp':'申通快递','courierId':4,'getTime':'2013-05-19T13:42:35','packageId':1177,'packageStatus':1,'parcelId':'112459512327','storeTime':'2013-05-19T10:56:28','terminalId':8,'userPhone':18960877052},{'boxId':1,'courierCorp':'圆通快递','courierId':3,'getTime':'2013-05-19T13:42:35','packageId':1185,'packageStatus':1,'parcelId':'112459512327','storeTime':'2013-05-19T13:41:41','terminalId':8,'userPhone':18960877052}]";
//            string te1 = @"[
//{'boxId':12,'courierCorp':'顺丰快递','courierId':2,'getTime':'2013-03-18T10:03:35','packageId':72,'packageStatus':1,'parcelId':
//'abcd12bacd','storeTime':'2013-03-18T10:01:59','terminalId':1,'userPhone':18960877052},
//{'boxId':12,'courierCorp':'顺丰快递','courierId':1,'getTime':'2013-05-17T15:54:49','packageId':71,'packageStatus':1,'parcelId':
//'abcd12341bacd','storeTime':'2013-03-18T09:48:48','terminalId':1,'userPhone':18960877052}]";
            pageContent = "{'info':" + te1 + "}";
            string JSON = "{'info':[{'title':'list1','url':'/upload/209/20120814163339890.png'},{'title':'离职申请表','url':'/upload/209/20120814163339968.xls'},{'title':'申请表','url':'/upload/209/20120814163340046.doc'},{'title':'室内','url':'/upload/209/20120814163340156.jpg'},{'title':'心电图','url':'/upload/209/20120814163340218.gif'},{'title':'新建 文本文档','url':'/upload/209/20120814163340281.txt'}]}";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(pageContent);
            object[] info = (object[])json["info"];
            Dictionary<string, object> val;
            Dictionary<int, string> Objs = null;
            Dictionary<int, string> fileOldName = null;

            List<ParcelInfo> listparcelinfo = new List<ParcelInfo>();

            for (int i = 0; i < info.Length; i++)
            {

                val = (Dictionary<string, object>)info[i];
                ParcelInfo parcelinfo = new ParcelInfo();
                foreach (KeyValuePair<string, object> str in val)
                {

                    if (str.Key.Equals("boxId"))
                    {
                        if (str.Value == null)
                            parcelinfo.boxId = "null";
                        else
                            parcelinfo.boxId = str.Value.ToString();
                    }
                    else if (str.Key.Equals("courierCorp"))
                    {
                        if (str.Value == null)
                            parcelinfo.courierCorp = "null";
                        else
                            parcelinfo.courierCorp = str.Value.ToString();
                    }
                    else if (str.Key.Equals("courierId"))
                    {
                        if (str.Value == null)
                            parcelinfo.courierId = "null";
                        else
                            parcelinfo.courierId = str.Value.ToString();
                    }
                    else if (str.Key.Equals("getTime"))
                    {
                        if (str.Value == null)
                            parcelinfo.getTime = "null";
                        else
                            parcelinfo.getTime = str.Value.ToString();
                    }
                    else if (str.Key.Equals("packageId"))
                    {
                        if (str.Value == null)
                            parcelinfo.packageId = "null";
                        else
                            parcelinfo.packageId = str.Value.ToString();
                    }
                    else if (str.Key.Equals("packageStatus"))
                    {
                        if (str.Value == null)
                            parcelinfo.packageStatus = "null";
                        else
                            parcelinfo.packageStatus = str.Value.ToString();
                    }
                    else if (str.Key.Equals("parcelId"))
                    {
                        if (str.Value == null)
                            parcelinfo.parcelId = "null";
                        else
                            parcelinfo.parcelId = str.Value.ToString();
                    }
                    else if (str.Key.Equals("storeTime"))
                    {
                        if (str.Value == null)
                            parcelinfo.storeTime = "null";
                        else
                            parcelinfo.storeTime = str.Value.ToString();
                    }
                    else if (str.Key.Equals("terminalId"))
                    {
                        if (str.Value == null)
                            parcelinfo.terminalId = "null";
                        else
                            parcelinfo.terminalId = str.Value.ToString();
                    }
                    else if (str.Key.Equals("userPhone"))
                    {
                        if (str.Value == null)
                            parcelinfo.userPhone = "null";
                        else
                            parcelinfo.userPhone = str.Value.ToString();
                    }
                    
                }
                listparcelinfo.Add(parcelinfo);
            }
            for (int i = 0; i < listparcelinfo.Count; i++)
            {

                string contentstring = "";
                contentstring = contentstring + "第" + (i + 1) + "条快件信息\r\n";
                contentstring = contentstring + "柜号ID:" + listparcelinfo[i].boxId + "\r\n";
                contentstring = contentstring + "终端ID:" + listparcelinfo[i].terminalId + "\r\n";
                contentstring = contentstring + "快件ID:" + listparcelinfo[i].parcelId + "\r\n";
                contentstring = contentstring + "快件状态:" + listparcelinfo[i].packageStatus + "\r\n";
                contentstring = contentstring + "邮员ID:" + listparcelinfo[i].courierId + "\r\n";
                contentstring = contentstring + "隶属公司:" + listparcelinfo[i].courierCorp + "\r\n";
                contentstring = contentstring + "联系电话:" + listparcelinfo[i].userPhone + "\r\n";
                contentstring = contentstring + "取件时间:\r\n" + listparcelinfo[i].getTime + "\r\n";
                contentstring = contentstring + "存件时间:\r\n" + listparcelinfo[i].storeTime + "\r\n";
                contentstring = contentstring + "\r\n\r\n";

                //System.Web.HttpContext.Current.Response.Write(string.Format(textTpl, model.FromUserName, model.ToUserName, ConvertDateTimeInt(DateTime.Now), "text", contentstring, 0));

                //Log.WriteLog(DefaultData.ACTION_TYPE_TEXTMSG_RESPONSE + string.Format(textTpl, model.FromUserName, model.ToUserName, ConvertDateTimeInt(DateTime.Now), "text", contentstring, 0));
                break;

            }

            Console.WriteLine(pageContent);

        }
        catch (Exception err)
        {
            Console.WriteLine(err.ToString());
        }
    }
    /// <summary>
    /// 请求WebService
    /// </summary>
    /// <param name="webserviceUrl"></param>
    /// <param name="charset"></param>
    /// <returns></returns>
    static string GetWebServiceContent(string webserviceUrl, string charset)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webserviceUrl);
        request.Method = "GET";
        request.Timeout = 60000;

        if (myCookieContainer == null)
            myCookieContainer = new CookieContainer();

        request.CookieContainer = myCookieContainer;


        HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        response.Cookies = myCookieContainer.GetCookies(request.RequestUri);


        Stream streamReceive = response.GetResponseStream();
        Encoding encoding = Encoding.Default;
        if (!string.IsNullOrEmpty(charset) && Encoding.GetEncoding(charset) != Encoding.Default)
        {
            encoding = Encoding.GetEncoding(charset);
        }

        StreamReader streamReader = new StreamReader(streamReceive, encoding);
        return streamReader.ReadToEnd();
    }

}